// pages/shop/components/shopDetails/shopDetails.js
var api = require('../../../../app/app.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL: 'https://www.sdhjfu.com/tooltwo/public/admin/',
    goodDetails: '',
    shopDetails: {},
    goodsId: '',
    userId: '',
    title: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { // 前面列表传来的id
    var goodsId = options.goodsId
    var goodsNam = JSON.parse(options.goodsNam)
    var that = this
    that.setData({
      goodsId: goodsId
    })
    // console.log('商品的ID' + goodsId)
    // 获取商品的信息
    wx.request({
      url: api.GoodsDetails,
      method: 'POST',
      data: {
        goodsId: goodsId
      },
      header: {
        'content-Type': 'application/x-www-form-urlencoded' // post请求头
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          shopDetails: res.data.goodsre
        })
      }
    })
    wx.setNavigationBarTitle({
      title: goodsNam
    })
  },
  butIntegral:function(){
    var that = this
    wx.getStorage({ // 获取本地缓存
      key: 'userReInfo',
      success(res) {
        // console.log(res.data)
        var userId = res.data.userId
        that.setData({
          userId: userId
        })
        wx.showModal({
          title: '确定兑换吗？',
          content: '',
          success(res) {
            if (res.confirm) {
              wx.request({ // 获取积分
                url: api.Createbill,
                data: {
                  userId: that.data.userId,
                  goodsId: that.data.goodsId
                },
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded' // post请求头
                },
                success: function (res) {
                 // console.log(res.data)
                  if (res.data.re == 'succ') {
                    wx.showModal({
                      title: '兑换成功',
                      content: '快去个人中心的"兑换码"里查看吧',
                      success(res) {
                        if (res.confirm) {
                          wx.redirectTo({
                            url: '/pages/me/components/code/code',
                          });
                        } else if (res.cancel) {}
                      }
                    })
                  } else if (res.data.re == 'fail'){
                    wx.showModal({
                      title: '兑换失败',
                      content: '快去推荐客户赢积分吧',
                      success(res) {
                        if (res.confirm) {} else if (res.cancel) { }
                      }
                    })
                  } else if (res.data.re == 'short') {
                    wx.showModal({
                      title: '积分不足',
                      content: '快去推荐客户赢积分吧',
                      success(res) {
                        if (res.confirm) {
                          wx.switchTab({
                            url: '/pages/recommend/recommend/recommend',
                          });
                        } else if (res.cancel) { }
                      }
                    })
                  }
                }
              })
            } else if (res.cancel) {
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})